﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using Ket_noi_toi_VPP_bang_mo_hinh_ba_lop.DAL;

namespace Ket_noi_toi_VPP_bang_mo_hinh_ba_lop
{
    public partial class FormKhachHang : Form
    {
        KhachHang KH = new KhachHang();

        public FormKhachHang()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = KH.SelectAll();
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            KH.Insert(this.tbTenKH.Text,this.tbDiaChi.Text,this.tbGT.Text,this.tbDT.Text,this.tbEmail.Text);
            dtgHienThi.DataSource = KH.SelectAll();
            Form1_Load(sender,e);
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            KH.Update(Convert.ToInt32(tbMaKH.Text),tbTenKH.Text, tbDiaChi.Text, tbGT.Text, tbDT.Text, tbEmail.Text);
            dtgHienThi.DataSource = KH.SelectAll();
            Form1_Load(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            KH.Delete(Convert.ToInt32(tbMaKH.Text));
            dtgHienThi.DataSource = KH.SelectAll();
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = KH.SelectID(Convert.ToInt32(tbMaKH.Text));
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgHienThi_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string ID;
            int dong;
            dong = e.RowIndex;//rows:hang,Cell:Ô
            ID = dtgHienThi.Rows[dong].Cells[2].Value.ToString();
            tbMaKH.Text = dtgHienThi.Rows[dong].Cells[0].Value.ToString();
            tbTenKH.Text = dtgHienThi.Rows[dong].Cells[1].Value.ToString();
            tbDiaChi.Text = dtgHienThi.Rows[dong].Cells[2].Value.ToString();
            tbGT.Text = dtgHienThi.Rows[dong].Cells[3].Value.ToString();
            tbDT.Text = dtgHienThi.Rows[dong].Cells[4].Value.ToString();
            tbEmail.Text = dtgHienThi.Rows[dong].Cells[5].Value.ToString();
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            this.tbMaKH.Clear();
            this.tbTenKH.Clear();
            this.tbDiaChi.Clear();
            this.tbGT.Clear();
            this.tbDT.Clear();
            this.tbEmail.Clear();
        }

        private void btGoBack_Click(object sender, EventArgs e)
        {
            Form1_Load(sender,e);
        }

        private void dtgHienThi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
